package com.bayihouse.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bayihouse.annotation.AuthCheck;
import com.bayihouse.common.BaseResponse;
import com.bayihouse.common.DeleteRequest;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.common.ResultUtils;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.exception.ThrowUtils;
import com.bayihouse.model.entity.House;
import com.bayihouse.model.request.house.HouseAddRequest;
import com.bayihouse.model.request.house.HouseQueryRequest;
import com.bayihouse.model.request.house.HouseUpdateRequest;
import com.bayihouse.model.vo.HouseVO;
import com.bayihouse.service.HouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/house")
@Slf4j
public class HouseController {


    @Resource
    private HouseService houseService;


    // endregion

    // region 增删改查

    /**
     * 创建用户
     *
     * @param houseAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Long> addHouse(@RequestBody HouseAddRequest houseAddRequest, HttpServletRequest request) {
        if (houseAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        House house = new House();
        BeanUtils.copyProperties(houseAddRequest, house);
        boolean result = houseService.save(house);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(house.getId());
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteHouse(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = houseService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     *
     * @param houseUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = AdministerConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateHouse(@RequestBody HouseUpdateRequest houseUpdateRequest,
                                             HttpServletRequest request) {
        System.out.println(houseUpdateRequest);
        if (houseUpdateRequest == null || houseUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        House house = new House();
        BeanUtils.copyProperties(houseUpdateRequest, house);
        boolean result = houseService.updateById(house);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<HouseVO> getHouseById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        House house = houseService.getById(id);
        ThrowUtils.throwIf(house == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(HouseVO.objToVo(house));
    }

    /**
     * 根据 id 获取包装类
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/vo")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<HouseVO> getHouseVOById(long id, HttpServletRequest request) {
        House response = houseService.getById(id);
        return ResultUtils.success(houseService.getHouseVO(response));
    }

    /**
     * 分页获取用户列表（仅管理员）
     *
     * @param houseQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<House>> listHouseByPage(@RequestBody HouseQueryRequest houseQueryRequest,
                                                     HttpServletRequest request) {
        long current = houseQueryRequest.getCurrent();
        long size = houseQueryRequest.getPageSize();
        Page<House> housePage = houseService.page(new Page<>(current, size),
                houseService.getQueryWrapper(houseQueryRequest));
        return ResultUtils.success(housePage);
    }

    /**
     * 分页获取用户封装列表
     *
     * @param houseQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<HouseVO>> listHouseVOByPage(@RequestBody HouseQueryRequest houseQueryRequest,
                                                         HttpServletRequest request) {
        if (houseQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = houseQueryRequest.getCurrent();
        long size = houseQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<House> housePage = houseService.page(new Page<>(current, size),
                houseService.getQueryWrapper(houseQueryRequest));
        System.out.println(housePage.getRecords());
        Page<HouseVO> houseVOPage = new Page<>(current, size, housePage.getTotal());
        List<HouseVO> houseVO = houseService.getHouseVOList(housePage.getRecords());
        houseVOPage.setRecords(houseVO);
        System.out.println(houseVOPage.getRecords());
        return ResultUtils.success(houseVOPage);
    }

    @PostMapping("/get/all")
    public BaseResponse<List<HouseVO>> getAllHouseVO(@RequestBody HouseQueryRequest houseQueryRequest) {
        return ResultUtils.success(houseService.getHouseVOList(houseService.list(houseService.getQueryWrapper(houseQueryRequest))));
    }

}
